package com.shoux.api.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.shoux.api.enm.AnSuoEnum;
import com.shoux.api.tools.MsgUtils;
import com.shoux.api.tools.RestTemplateUtil;
import com.shoux.api.web.entity.MsgEntity;
import com.shoux.api.web.entity.apiData.BasiclistEntity;
import com.shoux.api.web.entity.apiData.PostResEntity;
import com.shoux.api.web.entity.query.CompanyQuery;
import com.shoux.api.web.service.apiData.BasiclistService;
import com.shoux.api.web.service.apiData.PostResService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xiaoZhi
 * Date: 2022/3/25
 * Time: 15:09
 * Description: 企业controller
 */
@RestController
@RequestMapping("/company")
public class CompanyInfoController {

    @Autowired
    private RestTemplateUtil restTemplateUtil;

    @Autowired
    private BasiclistService basiclistService;

    @ApiOperation("调用外部远程接口进行企业名称模糊查询")
    @PostMapping("/getCompanyNamesByLikeName")
    public MsgEntity getCompanyNamesByLikeName(@RequestBody CompanyQuery companyQuery) {
        if (companyQuery != null && StringUtils.isNotEmpty(companyQuery.getName())) {
            JSONObject params = new JSONObject();
            params.put("name", companyQuery.getName());
            params.put("orgTypes", companyQuery.getOrgTypes());
            try {
                List<String> companyNames = basiclistService.getCompanyNameByLikeName(companyQuery.getName());
                if (companyNames == null || companyNames.size() == 0) {
                    String jsonObjectStr = restTemplateUtil.getBody(params, AnSuoEnum.R1105.getCode(), AnSuoEnum.R1105.getDesc()).toJSONString();
                    JSONArray read = (JSONArray) JSONPath.read(jsonObjectStr, "$.data");
                    return MsgUtils.successWhithData(read);
                }else{
                    JSONArray jsonArray = new JSONArray();
                    companyNames.stream().forEach(s->{
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("ENTNAME",s);
                        jsonArray.add(jsonObject);
                    });
                    return MsgUtils.successWhithData(jsonArray);
                }
            } catch (Exception e) {
                return MsgUtils.errorWhithMsg("调用外围接口异常，请联系开发人员！");
            }
        } else {
            return MsgUtils.errorWhithMsg("入参错误！请检查！");
        }

    }

    @ApiOperation("调用外部远程接口进行企业名称查询")
    @PostMapping("/getCompanyInfoByName")
    public MsgEntity getCompanyInfoByName(@RequestBody CompanyQuery companyQuery) {

        if (companyQuery != null && StringUtils.isNotEmpty(companyQuery.getName())) {
            JSONObject result = new JSONObject();
            BasiclistEntity basiclistEntity = basiclistService.getCompanyInfoByName(companyQuery);
            if (basiclistEntity == null) {
                JSONObject params = new JSONObject();
                params.put("enterpriseName", companyQuery.getName());
                try {
                    String jsonObjectStr = restTemplateUtil.getBody(params, AnSuoEnum.R1153V2.getCode(), AnSuoEnum.R1153V2.getDesc()).toJSONString();

                    JSONObject read = (JSONObject) JSONPath.read(jsonObjectStr, "$.data[0].basicList[0]");
                    result.put("name", read.getString("enterpriseName"));
                    result.put("creditcode", read.getString("creditCode"));
                    return MsgUtils.successWhithData(result);
                } catch (Exception e) {
                    e.printStackTrace();
                    return MsgUtils.errorWhithMsg("调用外围接口异常，请联系开发人员！");
                }
            } else {
                result.put("name", basiclistEntity.getEnterprisename());
                result.put("creditcode", basiclistEntity.getCreditcode());
            }
            return MsgUtils.successWhithData(result);
        } else {
            return MsgUtils.errorWhithMsg("入参错误！请检查！");
        }
    }
}
